<template>
  <!-- 账户管理 -->
  <div class="account-container">
    <PageCard
      title="查询条件"
      :showTitle="false"
    >
      <el-form
        :model="formData"
        label-position="right"
        :inline="true"
        size="small"
        ref="searchForm"
      >
        <el-form-item prop="roleId">
          <el-select
            class="w350"
            v-model="formData.roleId"
            filterable
            placeholder="请选择所属角色"
          >
            <el-option
              v-for="item in roleList"
              :key="item.id"
              :label="item.name"
              :value="item.id"
            >
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item prop="hospitalDepartId">
          <!-- <el-select
            class="w350"
            v-model="formData.hospitalDepartId"
            filterable
            placeholder="请选择所属科室"
          >
            <el-option
              v-for="item in departmentList"
              :key="item.id"
              :label="item.name"
              :value="item.id"
            >
            </el-option>
          </el-select> -->

          <Treeselect
            class="w350"
            v-model="formData.hospitalDepartId"
            placeholder="请选择所属科室"
            :options="departmentList"
            :load-options="loadOptions"
            :defaultOptions="true"
            searchPromptText="请输入科室名称或者首字母拼音"
            :async="true"
            noResultsText="暂无数据"
          />
        </el-form-item>
        <el-form-item prop="realName">
          <el-input
            class="w350"
            placeholder="请输入真实姓名"
            v-model="formData.realName"
          ></el-input>
        </el-form-item>
        <el-form-item>
          <el-button
            type="primary"
            @click="handleSearch()"
          >查询</el-button>
          <el-button @click="handleReset()">重置</el-button>
        </el-form-item>

      </el-form>
    </PageCard>
    <PageCard
      title="人员信息维护"
      icon_type="list"
      :hasCreate="true"
      btnText="新增人员"
      @create="handleAdd()"
    >
      <template #btns>
        <div class="beginner" v-if="hasguideCodes">
          <div
            class="ok"
            @click="handleBeginner(2)"
          >好的</div>
        </div>
        <el-button
          size="small"
          @click="handleImport"
        >导入人员</el-button>
        <el-button
          :disabled="multipleTable.length == 0 ? true : false"
          size="small"
          @click="handleExport"
        >批量导出</el-button>
        <el-button
          @click="handleDownloadCode()"
          :disabled="multipleTable.length == 0 ? true : false"
          size="small"
        >批量下载二维码</el-button>
      </template>
      <PageTable
        :data="data"
        :currentPage="currentPage"
        :pageSize="pageSize"
        :total="total"
        @selectionChange="selectionChange"
        @handleChange="handleSearch"
      >
        <el-table-column
          type="selection"
          header-align="center"
          align="center"
          width="55"
        >
        </el-table-column>
        <el-table-column
          prop="realName"
          label="真实姓名"
          width="120"
        >
        </el-table-column>
        <el-table-column
          prop="roleName"
          label="角色名称"
          width="200"
        > </el-table-column>
        <el-table-column
          prop="hospitalDepartName"
          width="200"
          show-overflow-tooltip
          label="科室名称"
        > </el-table-column>
        <el-table-column
          prop="mobile"
          width="160"
          label="手机号码"
        >
        </el-table-column>
        <el-table-column
          prop="idNumber"
          width="220"
          label="身份证号码"
        >
        </el-table-column>
        <el-table-column
          prop="jobNumber"
          label="工号"
          show-overflow-tooltip
        >
        </el-table-column>
        <el-table-column
          prop="email"
          label="常用邮箱"
          width="190"
          show-overflow-tooltip
        >
        </el-table-column>
        <el-table-column
          label="操作"
          fixed="right"
          width="230"
        >
          <template slot-scope="scope">
            <el-button
              type="text"
              :disabled="scope.row.roleCode == 'h001' ? true : false"
              @click.stop="handleEdit(scope.row)"
            >编辑</el-button>
            <el-button
              type="text"
              @click.stop="handleEdit(scope.row,'view')"
            >查看</el-button>
            <el-button
              type="text"
              class="text-success"
              @click.stop="handleGenerateQrCode(scope.row)"
            >下载二维码</el-button>
            <el-button
              type="text"
              :disabled="scope.row.roleCode == 'h001' ? true : false"
              class="text-danger"
              @click.stop="handleDel(scope.row)"
            >删除</el-button>
          </template>
        </el-table-column>
      </PageTable>
    </PageCard>
    <Child
      ref="child"
      @getDataList="handleSearch(currentPage,pageSize)"
    ></Child>
    <ImportFile
      @getDataList="handleSearch(currentPage,pageSize)"
      ref="ImportFile"
    ></ImportFile>
  </div>
</template>

<script>
import PageTable from "@/components/PageTable";
import PageCard from "@/components/PageCard";
import child from "./components/child.vue";
import importFile from "./components/importFile.vue";
import { roleList } from "@/api/setting1.js";
import * as department from "@/api/department.js";
import { hospitalDepartList } from "@/api/hospitalDepart.js";
import { settings } from "nprogress";
import { ASYNC_SEARCH } from "@riophae/vue-treeselect";
import * as Utils from "@/utils";
import * as Auth from "@/utils/auth";
import * as settingApi from "@/api/setting1";
export default {
  name: "Users",
  components: {
    PageTable: PageTable,
    PageCard: PageCard,
    Child: child,
    ImportFile: importFile,
  },
  data() {
    return {
      formData: {
        realName: "",
        roleId: "",
        hospitalDepartId: null,
      },
      currentPage: 1,
      pageSize: 10,
      total: 0,
      data: [],
      roleList: [], //角色列表
      multipleTable: [], //批量选择
      departmentList: [], //科室列表
      selectAsync: false,
      hasguideCodes: false,
      userInfo: {},
    };
  },
  created() {
    this.handleSearch(1, 10);
    this.handleGetRoleList();
    this.handleHospitalDepartList();
    this.userInfo = Auth.getUserInfo() ? JSON.parse(Auth.getUserInfo()) : {};
    this.hasguideCodes = this.userInfo.guideCodes[2] == 1 ? false : true;
  },
  methods: {
    //新手直营
    handleBeginner(index) {
      let params = this.userInfo.guideCodes;
      params[index] = 1;
      settingApi.updateGuide(params).then((res) => {
        if (res && res.success) {
          this.hasguideCodes = false;
          Auth.setUserInfo(JSON.stringify(this.userInfo));
        }
      });
    },
    async loadOptions({ action, searchQuery, callback }) {
      let _this = this;
      if (action === ASYNC_SEARCH) {
        let res = await hospitalDepartList({
          isPage: false,
          name: searchQuery,
        });
        if (res && res.success) {
          let departmentList = res.data.map((item) => {
            item.label = item.name;
            return item;
          });
          this.departmentList = Utils.treeDataTranslate(departmentList);
          callback(null, this.departmentList);
        }
      }
    },
    // 查询
    handleSearch(current, limit) {
      this.currentPage = current;
      this.pageSize = limit;
      const params = {
        ...this.formData,
        pageIndex: current || 1,
        pageSize: limit || 10,
      };
      // 调接口查询
      this.handleGetDataList(params);
    },
    //获取角色列表
    handleGetRoleList() {
      roleList({ isPage: false }).then((res) => {
        if (res && res.success) {
          this.roleList = res.data;
        }
      });
    },
    //获取科室列表
    handleHospitalDepartList(name) {
      hospitalDepartList({ isPage: false, name: name }).then((res) => {
        if (res && res.success) {
          let departmentList = res.data.map((item) => {
            item.label = item.name;
            return item;
          });
          this.departmentList = Utils.treeDataTranslate(departmentList);
        }
      });
    },
    //获取列表数据
    handleGetDataList(params) {
      department.sysUserStaffList(params).then((res) => {
        if (res && res.success) {
          this.data = res.data;
          this.total = res.total;
        }
      });
    },
    // 重置
    handleReset() {
      this.$refs["searchForm"].resetFields();
    },
    handleAdd() {
      this.$refs.child.showModal("add");
    },
    handleEdit(row,type='edit') {
      this.$refs.child.showModal(type, row);
    },
    //导入
    handleImport() {
      this.$refs.ImportFile.showModal();
    },
    //下载二维码
    handleGenerateQrCode(row) {
      department.generateQrCode(row.id).then((res) => {
        const link = document.createElement("a");
        const blob = new Blob([res], {
          type: "image/png", // "application/zip",
        });
        link.style.display = "none";
        link.href = URL.createObjectURL(blob);
        link.download = row.realName; // 下载后文件名
        document.body.appendChild(link);
        link.click();
        document.body.removeChild(link);
      });
    },

    //批量导出人员
    handleExport() {
      let params = {
        ids: this.multipleTable.map((item) => {
          return item.id;
        }),
      };
      department.exportList(params).then((res) => {
        const link = document.createElement("a");
        const blob = new Blob([res], {
          type: "application/vnd.ms-excel", // "application/zip",
        });
        link.style.display = "none";
        link.href = URL.createObjectURL(blob);
        link.download = "人员"; // 下载后文件名
        document.body.appendChild(link);
        link.click();
        document.body.removeChild(link);
      });
    },
    //批量导出二维码
    handleDownloadCode() {
      let params = this.multipleTable.map((item) => {
        return item.id;
      });
      department.generateQrCodeBatch(params).then((res) => {
        const link = document.createElement("a");
        const blob = new Blob([res], {
          type: "application/zip", // "application/zip",
        });
        link.style.display = "none";
        link.href = URL.createObjectURL(blob);
        link.download = "人员"; // 下载后文件名
        document.body.appendChild(link);
        link.click();
        document.body.removeChild(link);
      });
    },
    //删除用户
    handleDel(item) {
      this.$confirm("此操作将永久删除该用户, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          department.sysUserStaffRemove(item.id).then((res) => {
            if (res && res.success) {
              this.handleSearch(this.currentPage, this.pageSize);
              this.$message({
                type: "success",
                message: "删除成功!",
              });
            }
          });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除",
          });
        });
    },

    //重置密码
    handleResetPwd() {},
    //多选
    selectionChange(val) {
      this.multipleTable = val;
    },
  },
};
</script>

<style lang="scss" scoped>
.account-container {
  .btns {
    text-align: right;
  }
}
::v-deep .vue-treeselect__control {
  height: 32px;
  line-height: 32px;
}

::v-deep .vue-treeselect__placeholder,
::v-deep .vue-treeselect__single-value {
  line-height: 32px;
}
.beginner {
  position: absolute;
  width: 339px;
  height: 169px;
  background: url("../../../assets/top2.png") no-repeat center center;
  background-size: cover;
  top: -134px;
  right: -40px;
  z-index: 12;
  .ok {
    position: absolute;
    font-size: 14px;
    color: #33bbff;
    top: 90px;
    right: 45px;
    z-index: 13;
    cursor: pointer;
  }
}
</style>
